<?php

class DefaultController extends FrontController
{
    public function actionIndex()
    {
        $this->render('index');
    }

//    public function getGuestAllowedActions()
//    {
//        return array('index', 'captcha', 'page', 'signup', 'login',
//            'confirm', 'confirmAccountUpdate', 'confirmResetPassword', 'allowRegisterExistingAccount',
//            'confirmForUpgradeUserAccount','feedback','thankYouFeedback');
//    }

    public function accessRules()
    {
        return array(
        );
    }

    public function actionConfirm($code)
    {
        $result = FSM::run('User.user.confirm', array('code' => $code));
        if (!$result->getErrors()) {
            $this->render('confirm', array('error' => 0));
        }
        $this->render('confirm', array('error' => 1));
    }

    /**
     * TODO: Change the link in the confirm email to match with the action.
     * @param $code
     */
    public function actionConfirmForUpgradeUserAccount($code)
    {
        $result = FSM::run('User.user.confirmForUpgradeUserAccount', array('code' => $code));
        if (!$result->getErrors()) {
            $this->render('confirm', array('error' => 0));
        }
        $this->render('confirm', array('error' => 1));
    }

    public function actionConfirmAccountUpdate()
    {
        $user_id = $this->get('user_id', 0);
        $code = $this->get('code', '');

        $result = FSM::run('Filly.User.confirmAccountUpdate', array('user_id' => $user_id, 'code' => $code));
        $error = false;
        if ($result->hasErrors())
            $error = true;
        $this->render('confirm_account_update', array('error' => $error));
    }

    /**
     * For standard page (not for mobile page)
     * @param $code
     */
    public function actionConfirmResetPassword($code)
    {
        $result = FSM::run('Filly.User.confirmResetPassword', array('code' => $code));
        if (!$result->getErrors()) {
            //$this->render('confirm', array('error' => 0));
            $encryptedCode = md5($code . Settings::WEB_API_PUBLIC_KEY);
            $value = "{$code},{$encryptedCode}";
            $cookie = new CHttpCookie('validation_code', $value);
            $cookie->expire = time() + 86400; //a day = 86400 seconds
            Yii::app()->request->cookies['validation_code'] = $cookie;
            $this->redirect('/index.html#reset_password');
        }
        $this->render('confirm', array('error' => 1));
    }

    /**
     * This action call from email content sent by upgrading Filly to new Filly site.
     *
     * TODO: 1. Change action name to match with link in the email content
     * TODO: 2. Change the target link we want to redirect user to, it could be "fanclub.filly.com/existing.html"
     * TODO: 3. Change the render page when user entered an invalid code or something that's getting error.
     *
     * @param $code
     */
    public function actionAllowRegisterExistingAccount()
    {
        //$result = FSM::run('Filly.User.allowRegisterExistingAccount', array('code' => $code));
        Yii::import('User.models.User');
        $code = $this->get('code', '');
        $user = User::model()->find('validation_code=:code AND status=:status', array(
            ':code' => $code,
            ':status' => User::STATUS_EXISTING_ACCOUNT_WITH_MAIL_SENT
        ));
        if (!is_null($user)) {
            $encryptedCode = md5($user->email . Settings::WEB_API_PUBLIC_KEY);
            $value = "{$user->email},{$encryptedCode}";
            $cookie = new CHttpCookie('trasfering_email', $value);
            $cookie->expire = time() + 86400; //a day = 86400 seconds
            Yii::app()->request->cookies['trasfering_email'] = $cookie;
            $this->redirect('/index.html#transfer_account');
        }
        $this->render('confirm', array('error' => 1));
    }

    public function actionFeedback()
    {
        //$this->layout='//popup.r1';
        if (Yii::app()->request->IsPostRequest) {
            // save posted data
            $result = FSM::run('Site.SiteFeedback.save', $_POST);
            $model = $result->model;

            if ($this->post('ajax', '') == 'site-feedback-form') {
                echo $result->getActiveErrorMessages($result->model);
                Yii::app()->end();
            }
            if (!$result->hasErrors()) {
                //Email sending at here.
                $this->message = 'Success! Your submission has been saved!';
                $this->redirect($this->createUrl('thankYouFeedback', array('id' => $model->id)));
            }
        } else {
            // show edit form
            $id = $this->get('id', 0);
            if ($id == 0) {
                $model = new SiteFeedback();
            } else {
                $model = FSM::run('Site.SiteFeedback.get', array('id' => $id))->model;
            }
        }
        $this->render('feedback', array('model' => $model));
    }

    public function actionThankYouFeedback()
    {
        echo 'Thank you';
    }

    /**
     * Logout page.
     */
    public function actionLogout()
    {
        if (user()->isGuest)
            $this->redirect('/index.html');
        Yii::app()->user->logout();
        $this->redirect('/login.html');
    }

    public function actionConfirmResetPasswordForMobile($code)
    {
        $result = FSM::run('Filly.User.confirmResetPassword', array('code' => $code));
        if (!$result->getErrors()) {
            //$this->render('confirm', array('error' => 0));
            $model = $result->model;
            if (user()->isGuest)
                user()->setState('emailChange', $model->email);
            $this->redirect('/change-password.html');
        }
        $this->render('confirm', array('error' => 1));
    }

    public function changePassword()
    {

    }
    
    public function actionProtectionForm()
    {
        $model = new ProtectionForm();
        $this->render('protection_form', array('model' => $model));
    }

    public function actionGoDesktopSite(){
          if(Yii::app()->request->isAjaxRequest){
              $value = "desktop";
              $cookie = new CHttpCookie('PREFERRED_SITE', $value);
              $cookie->expire = time() + 365*86400; //a year=365* a day = 86400 seconds
              $cookie->path='/';
              $cookie->domain=".filly.com";
              Yii::app()->request->cookies['PREFERRED_SITE'] = $cookie;
          }
          else throw new CHttpException(404,'Request not found');

    }

}